package com.example.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.example.entity.User;
import com.example.entity.vo.TalkVo;
import com.example.interceptor.TokenNotValidation;
import com.example.mapper.TalkMapper;
import com.example.mapper.UserMapper;
import com.example.service.TalkService;
import com.example.utils.JwtUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;

@RestController
@RequestMapping("/talk")
public class TalkController {

    @Autowired
    TalkService talkService;

    @Autowired
    TalkMapper talkMapper;

    @Autowired
    UserMapper userMapper;

    @TokenNotValidation
    @PostMapping("/getall")
    public List<TalkVo> get(HttpServletRequest request) {
        String token = request.getHeader("Authorization");
        Integer userid = JwtUtil.getUserId(token.substring(7));
        CopyOnWriteArrayList<TalkVo> talkVos = talkService.selectBytouser(userid);
        int check = 0;
        try {
            for (TalkVo talkVo : talkVos) {
                if (check == talkVo.getFromuser()) {
                    talkVos.remove(talkVos.indexOf(talkVo));
                } else {
                    check = talkVo.getFromuser();
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return talkVos;
    }

    @TokenNotValidation
    @GetMapping("/selectByMyself")
    public List<User> selectByMyself(HttpServletRequest request) {
        String token = request.getHeader("Authorization");
        token = token.substring(7);
        Integer userId = JwtUtil.getUserId(token);
        return talkService.selectByMyself(userId);
    }


    //获取某两个人的未读聊天
    @TokenNotValidation
    @GetMapping("/getmsg/{fromuser}")
    public List<TalkVo> g(HttpServletRequest request, @PathVariable Integer fromuser) {
        String token = request.getHeader("Authorization");
        Integer touser = JwtUtil.getUserId(token.substring(7));
        return talkMapper.selectwhotowho(touser, fromuser);
    }

    /**
     * 未读转已读
     *
     * @param talkid
     */
    @TokenNotValidation
    @PutMapping("/unreadToRead/{talkid}")
    public void unreadToRead(@PathVariable Integer talkid) {
        talkService.unreadToRead(talkid);
    }

    /**
     * 获取已读信息
     *
     * @param request
     * @param fromuser
     * @return
     */
    @TokenNotValidation
    @GetMapping("/getReadInformation/{fromuser}")
    public List<TalkVo> getReadInformation(HttpServletRequest request, @PathVariable Integer fromuser) {
        String token = request.getHeader("Authorization");
        Integer touser = JwtUtil.getUserId(token.substring(7));
        return talkMapper.selectwhotowhowhenhasread(touser, fromuser);
    }

}
